package com.asktgapp.base;


import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.asktgapp.R;
import com.asktgapp.eventbus.UsedMachineEvent;
import com.asktgapp.user.fragment.UsenMachineFragment;

/**
 * time:2018/1/23
 * Create By ZhaoKai
 * Email: dczk@foxmail.com
 * Description:
 */

public abstract class CustomToolBrBaseActivity extends BaseActivity {

    public Toolbar toolbar;
    private TextView title;
    private AppBarLayout mBarLayout;
    private CheckBox left, right;
    private TextView mBackTv;

//    private UsenMachineFragment fragment;

    protected abstract Fragment getFragment();

    protected abstract String getToolTitle100();

    protected abstract String getToolTitle200();

    protected int getFragmentContainerId() {
        return R.id.fragmentContainer;
    }

    protected View getFragmentContainer() {
        return findViewById(getFragmentContainerId());
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_used);
//        PushAgent.getInstance(this).onAppStart();
        init();
    }

    protected void init() {
        setTitle(null);
        toolbar = (Toolbar) findViewById(com.asktgapp.modulebase.R.id.toolbar);
        setSupportActionBar(toolbar);
        left = (CheckBox) findViewById(R.id.toolbar_title_left);
        right = (CheckBox) findViewById(R.id.toolbar_title_right);
        mBarLayout = (AppBarLayout) findViewById(R.id.layout_app_bar);
        mBackTv = (TextView) findViewById(R.id.tv_back);
        mBackTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        FragmentManager fm = getSupportFragmentManager();
        Fragment content = fm.findFragmentById(R.id.fragmentContainer);
        if (content == null) {
            content = getFragment();
            if (content != null) {
                fm.beginTransaction().add(getFragmentContainerId(), content).commitAllowingStateLoss();
            }
        }
        left.setText(getToolTitle100());
        right.setText(getToolTitle200());

        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                left.setChecked(true);
                right.setChecked(false);
                UsedMachineEvent.postEvent(100);
            }

        });

        right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                left.setChecked(false);
                right.setChecked(true);
                UsedMachineEvent.postEvent(200);
            }
        });
    }

    /**
     * 设置标题
     *
     * @param titleString
     */
    public void setToolBarTitle(String titleString) {
        if (title == null)
            title = (TextView) findViewById(com.asktgapp.modulebase.R.id.toolbar_title);
        title.setText(titleString);
    }

    public void setBackNavigationIcon(int... icon) {
        getToolbar();
        toolbar.setNavigationIcon(icon != null && icon.length > 0 ? icon[0]
                : com.asktgapp.modulebase.R.mipmap.icon_back);
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    public void setCustomBackNavigation(boolean b) {
        if (b) {
            mBackTv.setVisibility(View.VISIBLE);
        } else {
            mBackTv.setVisibility(View.GONE);
        }
    }
}
